var votes = require('../models/vote'),
    ideas = require('../models/idea'),
    logger = require('../models/logger'),
    apiHandler = require('../services/api-handler'),
    activity = require('../models/activity');

exports.addVote = function (req, res) {
    var newVote,
        ideaId = req.body.ideaId,
        voteType = req.body.voteType,
        isUser = 0,
        userId = req.user._id,
        voteAction;
    if (voteType == "like") {
        voteAction = "Liked";
    } else {
        voteAction = "Disliked";
    }
    votes.findUserVoteOnIdea(userId, ideaId, function (err, vote) {
        if (err) {
            apiHandler.setErrorResponse('NO_VOTE_ON_IDEA_FOUND', res);
        } else if (vote == null) {
            newVote = new votes({
                user: userId,
                idea: ideaId,
                voteType: voteType
            });
            votes.createVote(newVote, function (err, vote) {
                if (err) {
                    apiHandler.setErrorResponse('VOTE_CREATION_ERROR', res);
                } else {
                    isUser = 0;
                    var newActivity = new activity({
                        activityType: voteAction,
                        idea: ideaId,
                        user: userId,
                        vote: vote
                    });
                    activity.createNewActivity(newActivity, function (err, activity) {
                        if (err) {
                            apiHandler.setErrorResponse('ACTIVITY_FAILED', res);
                        } else {
                            ideas.updateVoteData(ideaId, voteType, isUser, function (err, data) {
                                if (!err) {
                                    votes.findUserVoteOnIdea(userId, ideaId, function (err, vote) {
                                        if (err) {
                                            apiHandler.setErrorResponse('NO_VOTE_ON_IDEA_FOUND', res);
                                        } else {
                                            ideas.getIdeaById(ideaId, function (err, idea) {
                                                if (err) {
                                                    apiHandler.setErrorResponse('NO_IDEA_FOUND', res);
                                                } else {
                                                    var data = {};
                                                    if (vote)
                                                        data.voteType = vote.voteType;
                                                    else
                                                        data.voteType = "";
                                                    data.idea = idea;
                                                    apiHandler.setSuccessResponse(data, res);
                                                }
                                            });
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        } else {
            if (voteType == vote.voteType) {

                isUser = 2;
                votes.removeVote(ideaId, userId, voteType, function (err, data) {
                    if (err) {
                        apiHandler.setErrorResponse('NO_VOTE_ON_IDEA_FOUND', res);
                    } else {
                        ideas.updateVoteData(ideaId, voteType, isUser, function (err, data) {
                            if (!err) {
                                votes.findUserVoteOnIdea(userId, ideaId, function (err, vote) {
                                    if (err) {
                                        apiHandler.setErrorResponse('NO_VOTE_ON_IDEA_FOUND', res);
                                    } else {
                                        ideas.getIdeaById(ideaId, function (err, idea) {
                                            if (err) {
                                                apiHandler.setErrorResponse('NO_VOTE_ON_IDEA_FOUND', res);
                                            } else {
                                                var data = {};
                                                if (vote)
                                                    data.voteType = vote.voteType;
                                                else
                                                    data.voteType = "";
                                                data.idea = idea;
                                                apiHandler.setSuccessResponse(data, res);
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            } else {
                isUser = 1;
                vote.voteType = req.body.voteType;
                vote.save();
                var newActivity = new activity({
                    activityType: voteAction,
                    idea: ideaId,
                    user: req.user,
                    vote: vote._id
                });
                activity.createNewActivity(newActivity, apiHandler.handler.check(res, 'ACTIVITY_FAILED', function (activity) {
                    ideas.updateVoteData(ideaId, voteType, isUser, apiHandler.handler.check(res, 'ACTIVITY_FAILED', function (data) {
                        votes.findUserVoteOnIdea(userId, ideaId, apiHandler.handler.check(res, 'NO_VOTE_ON_IDEA_FOUND', function (vote) {
                            ideas.getIdeaById(ideaId, apiHandler.handler.check(res, 'NO_IDEA_FOUND', function (idea) {
                                var data = {};
                                if (vote)
                                    data.voteType = vote.voteType;
                                else
                                    data.voteType = "";
                                data.idea = idea;
                                apiHandler.setSuccessResponse(data, res);
                            }));
                        }));
                    }));
                }));
            }
        }
    });
};